package it.easycrm.pages;

import it.easycrm.HomePage;
import it.easycrm.data.dao.interfaces.UserDao;
import it.easycrm.data.dao.jpa.UserDaoImpl;
import it.easycrm.data.dataobjects.User;
import it.easycrm.models.UserModel;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * @author francesco
 */
public class LoginPage extends WebPage {

	
	@SpringBean
	private UserDao userDao;
	
	private UserModel user;
	public LoginPage() {
		this.user = new UserModel();
		Form<UserModel> login_form = new Form<UserModel>("login_form", new CompoundPropertyModel<UserModel>(user)) {
			 
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit() {
				// verify login 
				
				
				User u = userDao.getUserByUsername(user.getUsername());
				if(u==null){
					error("L'utente non esiste");
				}
				else{
					if(u.getPassword().equals(user.getPassword())){
						
						setResponsePage(HomePage.class);
					}
					else{
						error("Password errata");
					}
				}
				
			}
		};
		login_form.add(new RequiredTextField<String>("username"));
		login_form.add(new PasswordTextField("password"));
		login_form.setOutputMarkupId(true);
		add(login_form);
		FeedbackPanel fpanel = new FeedbackPanel("fpanel");
		fpanel.setOutputMarkupId(true);
		add(fpanel);
	}
	
}

